<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <el-form-item label="单据类型" prop="orderType">
          <el-select v-model="formData.orderType" placeholder="请选择单据类型">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.WAREHOUSE_ORDER_TYPE)"
                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"/>
          </el-select>
        </el-form-item>
        <el-form-item label="仓库单据Id" prop="storageOrderId">
          <el-input v-model="formData.storageOrderId" placeholder="请输入仓库单据Id"/>
        </el-form-item>
        <el-form-item label="质检来源类型" prop="sourceType">
          <el-select v-model="formData.sourceType" placeholder="请选择质检来源类型">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="质检来源类型名称" prop="sourceTypeName">
          <el-input v-model="formData.sourceTypeName" placeholder="请输入质检来源类型名称"/>
        </el-form-item>
        <el-form-item label="来源单号" prop="sourceNo">
          <el-input v-model="formData.sourceNo" placeholder="请输入来源单号"/>
        </el-form-item>
        <el-form-item label="业务类型" prop="businessType">
          <el-select v-model="formData.businessType" placeholder="请选择业务类型">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.WAREHOUSE_ORDER_BUSINESS_TYPE)"
                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"/>
          </el-select>
        </el-form-item>
        <el-form-item label="业务类型名称" prop="businessTypeName">
          <el-input v-model="formData.businessTypeName" placeholder="请输入业务类型名称"/>
        </el-form-item>
        <el-form-item label="出库库位id" prop="outStoreId">
          <el-input v-model="formData.outStoreId" placeholder="请输入出库库位id"/>
        </el-form-item>
        <el-form-item label="入库库位id" prop="inStoreId">
          <el-input v-model="formData.inStoreId" placeholder="请输入入库库位id"/>
        </el-form-item>
        <el-form-item label="物料id" prop="materialId">
          <el-input v-model="formData.materialId" placeholder="请输入物料id"/>
        </el-form-item>
        <el-form-item label="批次号" prop="batchNo">
          <el-input v-model="formData.batchNo" placeholder="请输入批次号"/>
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="formData.quantity" placeholder="请输入数量"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as OrderTaskApi from '@/api/warehouse/orderTask';

export default {
  name: "OrderTaskForm",
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        orderType: 2,
        storageOrderId: undefined,
        sourceType: undefined,
        sourceTypeName: undefined,
        sourceNo: undefined,
        businessType: undefined,
        businessTypeName: undefined,
        outStoreId: undefined,
        inStoreId: undefined,
        materialId: undefined,
        batchNo: undefined,
        quantity: undefined,
      },
      // 表单校验
      formRules: {},
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await OrderTaskApi.getOrderTask(id);
          this.formData = res.data;
          this.title = "修改仓库出/入库任务";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增仓库出/入库任务";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await OrderTaskApi.updateOrderTask(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await OrderTaskApi.createOrderTask(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        orderType: 2,
        storageOrderId: undefined,
        sourceType: undefined,
        sourceTypeName: undefined,
        sourceNo: undefined,
        businessType: undefined,
        businessTypeName: undefined,
        outStoreId: undefined,
        inStoreId: undefined,
        materialId: undefined,
        batchNo: undefined,
        quantity: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
